<?php

/**
 * @name: HOTN AnimeList
 * @author: HammerOnTheNail
 * @link: http://code.google.com/p/hotnanimelist/
 */

// Admin oldal címének meghatározása
$title .= " \ ".HOTN_ANIMELIST_ADMIN_MENU_FILE_EDIT;
$text = "";

if($_POST['action'] == "save")
{
	// Letöltési adatok lekérése - jelenlegi
	$download = $HOTN_ANIMELIST_DOWNLOAD->hotn_animelist_download_get_one($_POST['download_id']);
	
	// Project adatok lekérése
	$project = $HOTN_ANIMELIST_ADMIN_PROJECT->hotn_animelist_admin_project_get_one($download['download_project']);
	
	// Letöltési méret frissítése
	$update[] = "`download_filesize` = '".filesize(e_DOWNLOAD.$project['project_folder']."/".$download['download_type']."/".$download['download_url'])."'";
	
	// LEtöltés nevének frissítése
	if ($post['download_name']<>$download['download_name'])
	{
		$update[] = "`download_name` = '".$_POST['download_name']."'";
	}
	
	// Project állapotának frissítése
	if ($_POST['download_episode']<>$download['download_episode'])
	{
		$download_episode = $HOTN_ANIMELIST_DOWNLOAD->hotn_animelist_download_episode($_POST['download_type'],$_POST['download_episode']);

		$update[] = "`download_episode` = '".$download_episode."'";
		
		$sql->db_Update("hotn_animelist_project","*","`project_files` = `project_files`-".$download['download_episode']."+".$download_episode." WHERE `project_id` = '".$project['project_id']."'");
	}
	
	// Letöltés típusának frssítése és fileok áthelyezése
	if ($_POST['download_type']<>$download['download_type'])
	{
		$update[] = "`download_type` = '".$_POST['download_type']."'";
		
		// Mappák ellenőrzése
			if (!is_dir(e_DOWNLOAD.$project['project_folder']))
			{
				@mkdir(e_DOWNLOAD.$project['project_folder']);
			}
			if (!is_dir(e_DOWNLOAD.$project['project_folder']."/".$download['download_type']))
			{
				@mkdir(e_DOWNLOAD.$project['project_folder']."/".$download['download_type']);
			}
		
		$source = e_DOWNLOAD.$project['project_folder']."/".$download['download_type']."/".$download['download_url'];
		$dest = e_DOWNLOAD.$project['project_folder']."/".$_POST['download_type']."/".$download['download_url'];
		@rename($source,$dest);
	}
	
	$HOTN_ANIMELIST_DOWNLOAD->hotn_animelist_download_edit($update,$_POST['download_id']);
	$ns -> tablerender(HOTN_ANIMELIST_ADMIN_DEFAULT_ERROR_TITLE, HOTN_ANIMELIST_ADMIN_FILE_EDIT);
}

if($_POST['action'] == "edit" && $_POST['download_id'] <> "none")
{
	// Letöltés szerkesztése, csak amit lehet!
	$values = $HOTN_ANIMELIST_DOWNLOAD->hotn_animelist_download_get_one($_POST['download_id']); 
	
	// Alap adatok
	$values['action'] = e_SELF."?file.edit";
	$values['hidden'] = array("action" => "save","download_id"=>$_POST['download_id']);
	$values['project_submit'] = HOTN_ANIMELIST_DEFAULT_FORM_EDIT;
	$values['project_id_show'] = FALSE;
	$values['download_url_show'] = FALSE;
	$values['project_id'] = $values['download_project'];
	
	if (strpos($values['download_url'], "http")===0){
		$values['download_link'] = $values['download_url'];
	}
	
	include(e_PLUGIN."hotn_animelist/Admin/file/file.php");
}
else
{
	// Letöltések megjelenítése
	
	$data = array(
		"type" => "form",
		"method" => "post",
		"action" =>  e_SELF."?file.edit",
		"hidden" => array("action" => "edit"),
		"data" => array(
					array(
						"type" => "select",
						"name" => "download_id",
						"group" => "true",
						"value" => $HOTN_ANIMELIST_DOWNLOAD->hotn_animelist_download_get_all(),
						"text" => HOTN_ANIMELIST_ADMIN_FILE_DOWNLOAD_NAME
					),
					array(
						"type" => "submit",
						"value" => HOTN_ANIMELIST_DEFAULT_FORM_EDIT
					)
		)
	);
}

// Admin oldal tartalmának megjelenítése
$text .= "<div style='text-align: center;'>".$HOTN_ANIMELIST_FORM->show_form($data)."</div>";
?>